<template>
  <div class="app">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">面板</el-breadcrumb-item>
      <el-breadcrumb-item>面试技巧</el-breadcrumb-item>
      <el-breadcrumb-item>面试技巧</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="kappai">
      <el-row :gutter="24">
        <el-col :span="8">
          <span class="character"> 关键字</span>
          <el-input
            class="frame"
            v-model="queryInfo.keyword"
            placeholder="根据文章标题搜索"
          ></el-input>
        </el-col>
        <el-col :span="8">
          <span class="character"> 状态</span>
          <el-select v-model="queryInfo.state"  placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.state"
              :label="item.label"
              :value="item.state"
            >
            </el-option>
          </el-select>
          <el-button @click="clear" class="qingchu">清除</el-button>
          <el-button type="primary" @click="SearchState">搜索</el-button>
        </el-col>
        <el-col :span="8" class="jiqiao">
          <el-button type="success" icon="el-icon-edit" @click="addDialogVisible = true" >新增技巧</el-button>
        </el-col>
      </el-row>
      <el-alert type="info" show-icon> 数据一共 {{ total }} 条 </el-alert>
      <el-table :data="userlist" style="width: 100%">
        <el-table-column type="index" label="序号" width="90">
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="360">
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="180">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="180">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template slot-scope="scope">
            {{ scope.row.createTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
            <template v-solt="scope">
              <span v-if="scope.row.state  === 1">已启用</span>
              <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >预览</el-button
            >
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >禁用</el-button
            >
            <el-button
              @click="handleClick3(scope.row.id)"
              type="text"
              size="small"
              >修改</el-button
            >
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="yema"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[1, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="  prev, pager, next, sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 新增技巧 -->
    <el-dialog
  title="新增文章"
  :visible.sync="addDialogVisible"
  width="50%">
  <!-- 内容主题 -->
<el-form ref="addFormRef" :model="addForm" :rules="addFormRules"  label-width="80px">
  <el-form-item label="文章标题" prop="username">
    <el-input v-model="addForm.username"></el-input>
  </el-form-item>
    <el-form-item label="文章内容" prop="neirong">
      <quill-editor v-model="addForm"></quill-editor>
  </el-form-item>
</el-form>
  <!-- 按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          state: 1,
          label: '启用'
        },
        {
          state: 0,
          label: '禁用'
        }
      ],
      formInline: {},
      queryInfo: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      userlist: [],
      total: 0,
      addDialogVisible: false,
      // 添加技巧
      addForm: {
        username: ''
      },
      // 添加技巧验证
      addFormRules: {
        username: [
          { required: true, message: ' 请输入文章标题', trigger: 'blur' },
          {
            min: 1,
            message: '请输入文章标题',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    handleClick3(row) {
      console.log(row)
    },
    clear() {
      this.queryInfo.keyword = ''
      this.queryInfo.state = null
    },
    SearchState() {
      this.getUserList()
      // console.log(this.queryInfo.state)
    },
    async getUserList() {
      const { data } = await this.$http.get('/articles', {
        params: this.queryInfo
      })
      // if (res.meta.status !== 200) {
      //   return this.$message.error('获取用户列表失败！')
      // }
      // console.log(data)
      this.userlist = data.items
      this.total = data.counts
      // console.log(this.userlist)
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.getUserList()
    }
  }
}
</script>

<style lang="less" scoped>
.kappai {
  margin-top: 20px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.character {
  margin-left: 13px;
  font-weight: bold;
  padding-right: 8px;
}
.frame {
  width: 70%;
}
.qingchu {
  margin-left: 25px;
}
.jiqiao {
  text-align: right;
}
.thead {
  color: #909399;
  font-weight: 500;
}
.yema {
  text-align: right;
}
</style>
